package com.example.nfc;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.nfc.activity.AdminActivity;
import com.example.nfc.activity.SellerActivity;
import com.example.nfc.base.BaseNfcActivity;
import com.example.nfc.server.Const;
import com.example.nfc.server.network.http.HttpException;
import com.example.nfc.server.request.User;
import com.example.nfc.server.response.Respone;
import com.example.nfc.server.utils.CommonUtils;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseNfcActivity {
    private TextView ifo_NFC;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ifo_NFC = findViewById(R.id.ifo_NFC);
        // NFC适配器，所有的关于NFC的操作从该适配器进行
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (!ifNFCUse()) {
        }
        String uname = getData(Const.USERNAME,"");
        if(!TextUtils.isEmpty(uname)){
            username.setText(uname);
        }
        String pwd = getData(Const.PASSWORD,"");
        if(!TextUtils.isEmpty(pwd)){
            password.setText(pwd);
        }
        user = new User();
        boolean isAdmin = getData(Const.IS_ADMIN,true);
        if(isAdmin){
            user.setType("password");
            CommonUtils.setRadioButtonDrawable(this,rg,rb_1,0);
        }else{
            user.setType("seller");
            CommonUtils.setRadioButtonDrawable(this,rg,rb_2,1);
        }

        request(GET_TOKEN);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                int position = 0 ;
                if(checkedId==R.id.rb_1){
                    position = 0 ;
                    user.setType("password");
                    putData(Const.IS_ADMIN,true);
                }else{
                    position = 1 ;
                    user.setType("seller");
                    putData(Const.IS_ADMIN,false);
                }
                CommonUtils.setRadioButtonDrawable(MainActivity.this,rg,radioButton,position);
            }
        });
    }
    @Bind(R.id.rg)
    RadioGroup rg;
    @Bind(R.id.rb_1)
    RadioButton rb_1;
    @Bind(R.id.rb_2)
    RadioButton rb_2;
    @Bind(R.id.username)
    EditText username;
    @Bind(R.id.password)
    EditText password;
    @OnClick(R.id.login_btn)
    public void login_btnClick(){
        if(TextUtils.isEmpty(username.getText().toString())){
            shortToast("请输入账号!");
            return;
        }
        if(TextUtils.isEmpty(password.getText().toString())){
            shortToast("请输入密码!");
            return;
        }
        user.setUsername(username.getText().toString());
        user.setPassword(password.getText().toString());
        request(LOGIN_REQUEST);
        ProgressDialog.show(this,"正在登录中..");
    }

    private static final int LOGIN_REQUEST = 1001;
    private static final int GET_TOKEN = 1002;
    private User user;
    @Override
    public Object doInBackground(int requestCode, String parameter) throws HttpException {
        switch (requestCode){
            case GET_TOKEN:
                return action.getTokenRequest();
            case LOGIN_REQUEST:
                return action.loginRequest(user);
        }
        return super.doInBackground(requestCode, parameter);
    }

    @Override
    public void onSuccess(int requestCode, Object result) {
        super.onSuccess(requestCode, result);
        switch (requestCode){
            case GET_TOKEN:
                Respone token = (Respone) result;
                if(token.getCode()==200){
                    user.setDsc_token(token.getMessage());
                }
                break;
            case LOGIN_REQUEST:
                Respone respone = (Respone) result;
                ProgressDialog.disMiss();
                shortToast(respone.getMessage());
                if(respone.getCode()==200){
                    putData(Const.UID,respone.getUid());
                    putData(Const.USERNAME,user.getUsername());
                    putData(Const.PASSWORD,user.getPassword());
                    if(user.getType().equals("password")) {
                        startActivityAndFinish(AdminActivity.class);
                    }else {
                        startActivityAndFinish(SellerActivity.class);
                    }
                }
                break;
        }
    }

    @Override
    public void onFailure(int requestCode, int state, Object result) {
        super.onFailure(requestCode, state, result);
        switch (requestCode){
            case LOGIN_REQUEST:
                ProgressDialog.disMiss();
                break;
        }
    }

    /**
     * 检测工作,判断设备的NFC支持情况
     *
     * @return
     */
    protected Boolean ifNFCUse() {
        if (mNfcAdapter == null) {
            ifo_NFC.setText("温馨提示：当前设备不支持NFC！");
            return false;
        }
        if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) {
            ifo_NFC.setText("温馨提示：请在系统设置中先启用NFC功能！");
            return false;
        }
        return true;
    }
}

